package com.team.controller;

import com.team.pojo.DevUser;
import com.team.service.DevUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/")
public class DevUserController {
 @Autowired
 private DevUserService devUserService;
@RequestMapping("login")
 public String login( String inputCode,String username, String password, HttpSession session, Model model){
  //验证用户输入验证码是否匹配
 String code=session.getAttribute("codeValidate").toString();
 if (inputCode.equalsIgnoreCase(code)){
  //调用业务
    DevUser devUser=devUserService.login(username,password);
  if (devUser==null){  //登入失败
    model.addAttribute("error","用户名或密码错误");
    return "devlogin";
   }else {
    //使用session保存用户信息
    session.setAttribute("devUser",devUser);
    session.setMaxInactiveInterval(20*60);//20分钟有效
    return "developer/main";
   }
 }else {
    model.addAttribute("error","验证码不正确");
    return "devlogin";
  }
}
@RequestMapping("logout")  //退出登录
 public String logout(HttpSession session){
 //清楚session
 session.removeAttribute("devUser");
 return "devlogin";
}
}
